package classes.savedata
{
	import com.mttang.event.JEvent;
	import com.mttang.event.CommonEvent;
	import flash.events.*;
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.net.SharedObject;
	import flash.net.SharedObjectFlushStatus;
	import flash.events.NetStatusEvent;
	

	public class RockmanData extends Sprite
	{
		private static var my_so:SharedObject;
		
		public static function saveData(_data:Object):void//所有的场景通过这个接口获取数据对象;
		{
			//_data属性
			//_data.archive	存档名字
			//_data.userId  ID
			//_data.level   等级
			//_data.pet	    宠物
			//_data.gold	钱	
			//_data.props	道具
			//var dataRockman  : Object = _data;
			my_so = SharedObject.getLocal("rockman_loginInfo");
			if (my_so.data.rockmanFlag == "dare.mttang.com") {
				
				for (var i = 0; i < my_so.data.dataRockman.length; i++ ) 
				{
					//trace("已经有档了",i,my_so.data.dataRockman[i].userName);
					if (my_so.data.dataRockman[i] != null)
					{
						my_so.data.dataRockman[int(_data.id - 1)] = _data;						
					}
				}									
			}else 
			{	
				//trace("目前还没有存档");
				var _arr:Array = new Array();
				my_so.data.rockmanFlag = "dare.mttang.com";
				my_so.data.dataRockman = _arr;
				my_so.data.dataRockman[int(_data.id - 1)] = _data;	
				//trace("my_so.data.dataRockman.length:",my_so.data.dataRockman.length);					
			}
			
			var flushStatus:String = null;
            try {
                flushStatus = my_so.flush();
            } catch (error:Error) {
                trace("Error...Could not write SharedObject to disk");
            }
			if (flushStatus != null)
			{
				switch (flushStatus) 
				{
                    case SharedObjectFlushStatus.PENDING:
                        trace("Requesting permission to save object...");
                        my_so.addEventListener(NetStatusEvent.NET_STATUS, onFlushStatus);
                        break;
                    case SharedObjectFlushStatus.FLUSHED:
						//this.dispatchEvent(new JEvent(CommonEvent.SAVE_FILES_STATUS,{Status:true}));
                        trace("Value flushed to disk.");
                        break;
                }
			}
		}
		private static function onFlushStatus(event:NetStatusEvent):void {
            trace("User closed permission dialog...\n");
            switch (event.info.code) {
                case "SharedObject.Flush.Success":
                    trace("User granted permission -- value saved.");
					//this.dispatchEvent(new JEvent(CommonEvent.SAVE_FILES_STATUS,{Status:true}));
                    break;
                case "SharedObject.Flush.Failed":
                    trace("User denied permission -- value not saved.");
					//this.dispatchEvent(new JEvent(CommonEvent.SAVE_FILES_STATUS,{Status:false}));
                    break;
            }
            my_so.removeEventListener(NetStatusEvent.NET_STATUS, onFlushStatus);
        }
/****************************************************************/
	}
}